<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Friend_del extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function __construct()
    {
		parent::__construct();
		$this->load->model('friendtop_model');
		session_start();
	}
	public function index()
	{
		//$temp=$this->session->userdata('access_token');
		$temp=$_SESSION['token']['access_token'];
		
		//var_dump($temp);
		$code=0;
		$result='';
		$response=array();
        if ( empty($temp))   //如果用户没登录
        {
            $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
            $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
            echo "<meta http-equiv=refresh content='0; url=$code_url'>";  //跳转到授权页面
        }
        else
		{
			$fid_del=$this->input->post("fid_del");	//要删除的好友的ID
			//var_dump($fid_del);

			$c = new SaeTClientV2( WB_AKEY , WB_SKEY ,$temp );

			$uid_get=$c->get_uid();   //获取当前登录用户的UID
			$uid=$uid_get['uid'];
			
			//清除session中该好友的微博信息
			if(isset($_SESSION['weibos']))
			{
				$fid_keys=array_keys($_SESSION['weibos']);	//将原session中的微博信息对应的keys(fids)提取出来
				$location_session=array_search($fid_del,$fid_keys);	//确定指定的fuid在原来session中的位置
				array_splice($_SESSION['weibos'],$location_session,1);	//删除指定的fuid在原session中的微博信息
			}

			/*重置数据库中的信息
			 * 将wb_custom中的fids字段重置
			**/
			
			$sql_fids="select fids from wb_appFriendsView_custom where uid=$uid";
			$fids_db=$this->db->query($sql_fids);
			$fids_db=$fids_db->result_array();
			
			$fids_db_arr=explode("-",$fids_db[0]['fids']);	//将取出的fids以“-”分隔成数组fid_db_arr
			
			$location_fids=array_search($fid_del,$fids_db_arr);	//找出该uid在fids中的位置
			
			array_splice($fids_db_arr,$location_fids,1);	//从fids中删除该uid
			
			$fids_new=implode("-",$fids_db_arr);
			
			$sql_update="UPDATE wb_appFriendsView_custom set fids=$fids_new where uid=$uid";
			$this->db->query($sql_update);

			$code=1;
			$result='delete successfully.';
				
		}

		$response=array
			(
				'code'=>$code,
				'result'=>$result
			);

		echo json_encode($response);
	}
}
